package cn.kim.edu.info.manager.controller;

import cn.kim.edu.info.manager.domain.Student;
import cn.kim.edu.info.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {
    //将学生对象，传递给方法服务层
    private StudentService studentService = new StudentService();
    private Scanner sc = new Scanner(System.in);

    //用来开启学生管理系统，并且展示学生管理系统菜单
    public void start() {
        //键盘录入选择
        //Scanner sc = new Scanner(System.in);
        studentLoop:
        while (true) {
            //打印职员管理菜单
            System.out.println("------------欢迎使用<学生>管理系统----------------");
            System.out.println("请输入你的选择：1.添加学生 2.删除学生 3.修改学生 4.查看学生 5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudentById();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updateStudent();
                    break;
                case "4":
                    //System.out.println("查看学生");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢使用Kim学生管理系统！");
                    break studentLoop;
                default:
                    System.out.println("你的输入有误！请重新输入");
            }
        }
    }

    //修改信息
    public void updateStudent() {

        String updateId = inputStudentId();
        Student newStu=inputStudentInfo(updateId);
        studentService.updateStudent(updateId, newStu);
        System.out.println("学生信息修改成功！");
    }

    //删除学生
    private void deleteStudentById() {
        String delId = inputStudentId();
        studentService.deleteStudentById(delId);
        System.out.printf("学生信息删除成功！！！");
    }

    //查看学生信息方法
    public void findAllStudent() {
        //调用方法层
        Student[] sten = studentService.findAdllStudent();
        //判断数组内存地址是否为null
        if (sten == null) {
            System.out.println("查无学生信息！");
            return;
        }
        //遍历数组，获取学生信息并且打印在控制台
        System.out.println("学生ID\t\t姓名\t\t年龄\t\t生日");
        for (int i = 0; i < sten.length; i++) {
            Student sts = sten[i];
            if (sts != null) {
                System.out.println(sts.getId() + "\t" + sts.getName() + "\t" + sts.getAge() + "\t" + sts.getBirthday());
            }
        }
    }

    //添加学生的方法
    public void addStudent() {
        String id;
        //1.键盘接收学生信息
        //Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入学生ID：");
            id = sc.next();
            boolean flag = studentService.isExists(id);
            if (flag) {
                System.out.println("ID号已占用，请重新输入");
            } else {
                break;
            }
        }

        Student stu = inputStudentInfo(id);
        boolean result = studentService.addStudent(stu);
        if (result) {
            System.out.println("添加成功！");
        } else {
            System.out.println("添加失败！");
        }
    }

    //键盘录入
    public String inputStudentId() {
        String id;
        while (true) {
            System.out.println("请输入学生ID：");
            id = sc.next();
            boolean exists = studentService.isExists(id);
            if (!exists) {
                System.out.println("你输入的ID号不存在");
            } else {
                break;
            }
        }
        return id;
    }

    //方法抽离  键盘录入学生信息
    public Student inputStudentInfo(String id) {
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        String age = sc.next();
        System.out.println("请输入学生生日：");
        String birthday = sc.next();
        //将学生信息封装为学生对象 并通过对象名.set方法获取输入的值；
        Student stu = new Student();
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);
        return stu;
    }


}
